const axios = require("axios");
const Result = require("../../dbs/models/result");
const Product = require("../../dbs/models/product");
const Suggest = require("../../dbs/models/suggest");
const Hotplace = require("../../dbs/models/hotplace");
const Hotcity = require("../../dbs/models/hotcity");
const Tab = require("../../dbs/models/tab");
const Menu = require("../../dbs/models/menu");

module.exports = {
  // 有格调
  get tabs() {
    return async ctx => {
      let { keyword } = ctx.query;
      if (keyword) {
        // 本地数据库查询
        let res = await Tab.find({ type: keyword }, "type value");
        if (res.length > 0) {
          return (ctx.body = {
            code: 0,
            msg: "获取成功",
            data: {
              count: res.length,
              pois: res.map(item => {
                return {
                  type: item.type,
                  value: item.value
                };
              })
            }
          });
        } else {
          ctx.throw(400, "暂无数据");
        }
      } else {
        ctx.throw(400, "请输入正确参数");
      }
    };
  },
  // 搜索建议
  get suggest() {
    return async ctx => {
      let { city, keyword } = ctx.query;
      if (city && keyword) {
        let {
          status,
          data: { top }
        } = await axios.get("http://cp-tools.cn/search/top", {
          params: {
            input: keyword,
            city
          }
        });
        if (status === 200 && top.length > 0) {
          return (ctx.body = {
            code: 0,
            msg: "获取成功",
            data: top
          });
        } else {
          // 请求美团接口
          let {
            status,
            data: {
              data: { suggestItems }
            }
          } = await axios.get("https://www.meituan.com/ptapi/suggest", {
            params: {
              keyword
            }
          });
          if (status === 200 && suggestItems.length > 0) {
            let resultArr = suggestItems.map(item => {
              return {
                name: item.query,
                type: "from 美团"
              };
            });
            return (ctx.body = {
              code: 0,
              msg: "获取成功",
              data: resultArr
            });
          } else {
            let reg = eval(`/^${keyword}/i`);
            let result = await Suggest.find({ keyword: reg });
            if (result.length > 0) {
              ctx.body = {
                code: 0,
                msg: "获取成功",
                data: {
                  count: res.length,
                  suggestItems: res.map(item => {
                    return {
                      name: item.name,
                      type: item.type
                    };
                  })
                }
              };
            } else {
              ctx.throw(400);
            }
          }
        }
      } else {
        ctx.throw(400, "请输入正确参数");
      }
    };
  },
  get product() {
    return async ctx => {
      let { id } = ctx.params;
      if (!id) {
        ctx.throw(400, "请输入正确的参数");
      }
      let res = await Product.findOne({ id });
      if (res) {
        return (ctx.body = {
          code: 0,
          msg: "获取成功",
          data: {
            product: res.product,
            more: res.more
          }
        });
      } else {
        ctx.throw(400, "暂无数据");
      }
    };
  },
  get hotplace() {
    return async ctx => {
      let res = await Hotplace.findOne();
      if (res) {
        return (ctx.body = {
          code: 0,
          msg: "获取成功",
          data: res.value
        });
      } else {
        ctx.throw(400, "暂无数据,请联系开发者");
      }
    };
  },
  get hotcity() {
    return async ctx => {
      let res = await Hotcity.find();
      if (res.length > 0) {
        return (ctx.body = {
          code: 0,
          msg: "获取成功",
          data: res.map(item => {
            return {
              province: item.province,
              name: item.name,
              id: item.id
            };
          })
        });
      } else {
        ctx.throw(400, "暂无数据");
      }
    };
  },
  // 菜单数据
  get menu() {
    return async ctx => {
      let result = await Menu.findOne();
      if (result) {
        return (ctx.body = {
          code: 0,
          msg: "获取成功",
          data: result.menu
        });
      } else {
        return ctx.throw(400, "暂无数据");
      }
    };
  },
  get result() {
    return async ctx => {
      let { keyword } = ctx.query;
      if (!keyword) {
        ctx.throw(400, "请输入正确的参数");
      }
      let res = await Result.findOne({ keyword });
      if (res) {
        return (ctx.body = {
          code: 0,
          msg: "获取成功",
          data: {
            count: res.value.length,
            result: res.value
          }
        });
      } else {
        ctx.throw(400, "暂无数据");
      }
    };
  }
};
